// ============================================================================
// User 实体定义
// ============================================================================

import {
  Entity,
  PrimaryColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  BeforeInsert
} from 'typeorm'
import { v4 as uuidv4 } from 'uuid'

/**
 * 用户实体
 */
@Entity('users')
@Index(['uid'], { unique: true })
@Index(['nickname'])
@Index(['isVip'])
@Index(['lastUpdated'])
export class User {
  @PrimaryColumn({ type: 'varchar' })
  id: string

  @Column({ unique: true, type: 'varchar' })
  uid: string

  @Column('varchar')
  nickname: string

  @Column({ nullable: true, type: 'varchar' })
  avatar?: string

  @Column({ name: 'is_vip', type: 'boolean', default: false })
  isVip: boolean

  @Column({ name: 'followers_count', type: 'integer', nullable: true })
  followersCount?: number

  @Column({ name: 'following_count', type: 'integer', nullable: true })
  followingCount?: number

  @Column({ name: 'statuses_count', type: 'integer', nullable: true })
  statusesCount?: number

  @Column({ type: 'text', nullable: true })
  description?: string

  @Column({ nullable: true, type: 'varchar' })
  location?: string

  @Column({ name: 'verified_type', type: 'integer', nullable: true })
  verifiedType?: number

  @Column({ name: 'verified_reason', nullable: true, type: 'varchar' })
  verifiedReason?: string

  @Column({ name: 'last_updated', type: 'datetime', nullable: true })
  lastUpdated?: Date

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date

  @BeforeInsert()
  generateId() {
    if (!this.id) {
      this.id = uuidv4()
    }
  }

  /**
   * 检查是否为VIP用户
   */
  isVipUser(): boolean {
    return this.isVip
  }

  /**
   * 检查是否为认证用户
   */
  isVerified(): boolean {
    return this.verifiedType !== null && this.verifiedType !== undefined
  }

  /**
   * 获取粉丝数量
   */
  getFollowersCount(): number {
    return this.followersCount || 0
  }

  /**
   * 获取关注数量
   */
  getFollowingCount(): number {
    return this.followingCount || 0
  }

  /**
   * 获取微博数量
   */
  getStatusesCount(): number {
    return this.statusesCount || 0
  }

  /**
   * 更新用户信息
   */
  updateInfo(info: Partial<User>): void {
    if (info.nickname) this.nickname = info.nickname
    if (info.avatar) this.avatar = info.avatar
    if (info.isVip !== undefined) this.isVip = info.isVip
    if (info.followersCount !== undefined) this.followersCount = info.followersCount
    if (info.followingCount !== undefined) this.followingCount = info.followingCount
    if (info.statusesCount !== undefined) this.statusesCount = info.statusesCount
    if (info.description) this.description = info.description
    if (info.location) this.location = info.location
    if (info.verifiedType !== undefined) this.verifiedType = info.verifiedType
    if (info.verifiedReason) this.verifiedReason = info.verifiedReason
    
    this.lastUpdated = new Date()
  }

  /**
   * 转换为用户信息格式（向后兼容）
   */
  toUserInfo() {
    return {
      uid: this.uid,
      nickname: this.nickname,
      avatar: this.avatar || '',
      isVip: this.isVip,
      followersCount: this.followersCount,
      followingCount: this.followingCount,
      statusesCount: this.statusesCount,
      description: this.description,
      location: this.location,
      verifiedType: this.verifiedType,
      verifiedReason: this.verifiedReason
    }
  }

  /**
   * 从用户信息创建用户实体
   */
  static fromUserInfo(userInfo: any): User {
    const user = new User()
    user.uid = userInfo.uid
    user.nickname = userInfo.nickname
    user.avatar = userInfo.avatar
    user.isVip = userInfo.isVip || false
    user.followersCount = userInfo.followersCount
    user.followingCount = userInfo.followingCount
    user.statusesCount = userInfo.statusesCount
    user.description = userInfo.description
    user.location = userInfo.location
    user.verifiedType = userInfo.verifiedType
    user.verifiedReason = userInfo.verifiedReason
    return user
  }

  /**
   * 转换为前端显示格式
   */
  toUserDataFormat(): any {
    return {
      id: this.id,
      uid: this.uid,
      nickname: this.nickname,
      avatar: this.avatar,
      isVip: this.isVip,
      followersCount: this.followersCount || 0,
      followingCount: this.followingCount || 0,
      statusesCount: this.statusesCount || 0,
      description: this.description,
      location: this.location,
      verifiedType: this.verifiedType,
      verifiedReason: this.verifiedReason,
      isVerified: this.isVerified(),
      lastUpdated: this.lastUpdated,
      createdAt: this.createdAt,
      updatedAt: this.updatedAt
    }
  }
}
